<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        * {
            margin: 0;
            padding: 0
        }

        ul {
            list-style: none
        }

        body {
            background-color: #333;
        }

        .nav {
            width: 800px;
            height: 42px;
            margin: 100px auto;
            background: url(img/jindouyun/rss.png) right center no-repeat;
            background-color: #fff;
            border-radius: 10px;
            position: relative;
        }

        .nav li {
            width: 83px;
            height: 42px;
            text-align: center;
            line-height: 42px;
            float: left;
            cursor: pointer;
        }

        .nav span {
            position: absolute;
            top: 0;
            left: 0;
            width: 83px;
            height: 42px;
            background: url(img/jindouyun/cloud.gif) no-repeat;
        }

        ul {
            position: relative;
        }
    </style>
</head>
<body>
<div class="nav">
    <span id="cloud"></span>
    <ul id="navBar">
        <li>北京校区</li>
        <li>上海校区</li>
        <li>广州校区</li>
        <li>深圳校区</li>
        <li>武汉校区</li>
        <li>关于我们</li>
        <li>联系我们</li>
        <li>招贤纳士</li>
    </ul>
</div>
<script>
    //找人
    var cloud = document.getElementById("cloud");
    var navBar = document.getElementById("navBar");
    var lis = navBar.children;
    var lastPosition = 0;
    //给每一个li绑定鼠标经过事件 鼠标经过调用动画函数让云彩飞过来
    for (var i = 0; i < lis.length; i++) {
        lis[i].onmouseover = function () {
            //目标就是当前li的offsetLeft
            var target = this.offsetLeft;
            animate(cloud, target);
        }
        lis[i].onmouseout = function () {
            animate(cloud, lastPosition);
        }
        lis[i].onclick = function () {
            lastPosition = this.offsetLeft;
        }
    }


    function animate(obj, target) {
        clearInterval(obj.timer);
        obj.timer = setInterval(function () {
            //step = (target - leader) / 10
            //leader = leader + step
            var leader = obj.offsetLeft;
            var step = (target - leader) / 10;
            /*if (step > 0) {
             step = Math.ceil(step);
             }
             if (step < 0) {
             step = Math.floor(step);
             }*/
            step = step > 0 ? Math.ceil(step) : Math.floor(step);
            leader = leader + step;
            console.log("target: " + target + "leader: " + leader + "step: " + step);
            if (leader != target) {
                obj.style.left = leader + "px";
            } else {
                clearInterval(obj.timer);
            }
        }, 10)
    }
</script>
</body>
</html>
